<template>
  <div>
    <el-row type="flex" justify="end">
      <!-- 循环年 -->
      <el-select v-model="currentYear" size="small" style="width: 120px" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
      <!-- 循环列 -->
      <el-select v-model="currentMonth" size="small" style="width: 120px; margin-left: 10px" @change="dateChange">
        <!-- 循环1-12个月 -->
        <el-option v-for="item in 12" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template #dateCell="{ date, data }">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    // 从日期字符串中得到天
    getDay(value) {
      const day = value.split('-')[2]
      return day.startsWith('0') ? day.substr(1) : day
    }
  },
  props: {
    // 可以显示任何时间日期的日历
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      currentYear: '', // 当前的年
      currentMonth: '', // 当前的月份
      currentDate: '', // 当前的时间日期对象
      yearList: [] // 待循环的年：当前年份的前5年和后5年
    }
  },
  created() {
    // 处理起始时间
    // 组件要求起始时间必须是 周一开始 以一个月为开始
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentYear = this.startDate.getFullYear()
    // 2023===> [2019,2020,2021,2022,2023,2024,2025,2026,2027,2028]
    /* for (let i = this.currentYear - 5; i <= this.currentYear + 5; i++) {
      this.yearList.push(i)
    } */
    // 循环的年
    this.yearList = Array.from(Array(11), (value, index) => this.currentYear + index - 5)
    // 当前日历要显示的日期
    this.currentDate = this.startDate
  },
  methods: {
    // 年月份改变之后都回到当前月的1号
    dateChange() {
      const year = this.currentYear
      const month = this.currentMonth
      this.currentDate = new Date(`${year}/${month}/1`) // 以当前月的1号为起始
    },
    // 是否是休息日
    // value是时间日期对象
    isWeek(value) {
      return value.getDay() === 6 || value.getDay() === 0
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-calendar-day {
    height: auto;
  }
  .el-calendar-table__row td,
  .el-calendar-table tr td:first-child,
  .el-calendar-table__row td.prev {
    border: none;
  }
  .el-calendar-table td.is-selected .text {
    background: #409eff;
    color: #fff;
    border-radius: 50%;
  }
  .el-calendar__header {
    display: none;
  }
}

.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
</style>
